let fs = require("fs");
let os = require("os");

let writestream = fs.WriteStream('./input.txt', 'utf-8');
let buf = 'hello world';
console.log(Buffer.from(buf));  //打印出来输出两次才有chunk
writestream.write(buf.toString(buf));    
let startMem = os.freemem();  //开始内存

let data = fs.readFileSync('./input.txt', { encoding: 'utf8', flag: 'r' });;
let readStream = fs.createReadStream('./input.txt', { encoding: "utf-8", highWaterMark: 3 });

readStream.on('data', function (chunk) {
    console.log('22' + chunk);
    data += chunk;
})

readStream.on('end', function () {
    console.log("流完了");
    let endMem = os.freemem();     //结束内存
    console.log('损耗内存' + (startMem - endMem) / 1024 / 1024);
})


readStream.on('close', function () {
    console.log("流结束了");
})

console.log("end");


